# Copyright (C) 2007 "dbsolution sas" 
#
# This file is part of Weebotee.
#
# Weebotee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Weebotee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Weebotee.  If not, see <http://www.gnu.org/licenses/>.

#import unittest
from django.test import TestCase
import settings
from ..libs.utils import plug_core
from apps.core.models.element import Element

## THIS TEST CASE IS DESIGNED JUST FOR /libs/utils/plugcore.py ##

class PlugCore(TestCase):
    fixtures = ['resources/test.json']
    def SetUp(self):
        # avoid real action on some critical functions
        #settings.FAKE = True  # do this in all Tests..
        pass
    
    def testscan_applications(self):
        # test for unexistent method
        mlist = plug_core.scan_applications("prova")
        self.assert_(mlist=={},"seem that prova method exist somewhere!")
        mlist = plug_core.scan_applications("admin_link")
        self.assert_(mlist["dbframework.apps.speakcp"]=={'url': '/plugin/speakcp', 'icon': '/media/admin/icon-menu-speakcp.gif', 'name': 'speakcp', 'title': 'amministrazione di speakcp'}, "something change in speakcp admin_link!!")
        self.assert_(mlist["dbframework.apps.witch"]=={'url': '/plugin/witch', 'icon': '/media/admin/icon-menu-witch.gif', 'name': 'Fatturazioni', 'title': 'Amministrazione delle fatturazioni'}, "something seem changed into witch admin_link")

    def test_get_all_active_models(self):
        mlist = plug_core.get_all_active_models()
        self.assert_(mlist, "No models found!")

    def test_get_model_from_etype(self):
        m = plug_core.get_model_from_etype("core:Element")
        self.assert_(m, "get_model_from_etype returns nothing")
        self.assert_(m.__class__.__name__ == "ModelBase", "get_model_from_etype returned a %s instead of ModelBase"%m.__class__.__name__)
        
    def test_load_plug_urls(self):
        r = plug_core.load_plug_urls()
        self.assert_(r, "load_plug_urls returns nothing")

    def test_load_plug_services_urls(self):
        r = plug_core.load_plug_services_urls()
        self.assert_(r, "load_plug_services_urls returns nothing")

    def test_get_reverse_related_element(self):
        e = Element.objects.all()[0]
        r = plug_core.get_reverse_related_element(e)
        self.assert_(r, "get_reverse_related_element returns nothing")

    def test_get_entity_part(self):
        r = plug_core.get_entity_part("core:Article")
        self.assert_(r, "get_entity_part returns nothing")

    def test_get_reverse_related_set(self):
        e = Element.objects.all()[0]
        r = plug_core.get_reverse_related_set(e)
        self.assert_(r, "get_reverse_related_set returns nothing")

    def test_get_reverse_related_app(self):
        e = Element.objects.all()[0]
        r = plug_core.get_reverse_related_app(e)
        self.assert_(r, "get_reverse_related_app returns nothing")

    def test_get_section_part(self):
        r = plug_core.get_section("core:Article")
        self.assert_(r, "get_section_part returns nothing")

    def test_get_method_in_app(self):
        r = plug_core.get_method_in_app("witch", "admin_link")
        self.assert_(r, "get_method_in_app returns nothing")
        r = plug_core.get_method_in_app("adgagad", "daaaa")
        self.assert_(not r, "get_method_in_app returns something with a fake app!")
        r = plug_core.get_method_in_app("witch", "adgdad")
        self.assert_(not r, "get_method_in_app returns something with a fake method!")

    def test_resolve_element(self):
        e = Element.objects.all()[0]
        r = plug_core.resolve_element(e)
        self.assert_(r, "resolve_element returns nothing!")

    def test_get_tmpl_from_plug(self):
        e = Element.objects.all()[0]
        r = plug_core.get_tmpl_from_plug("short", e)
        self.assert_(r, "get_tmpl_from_plug returns nothing!")


    def test_build_sections_list(self):
        l = plug_core.scan_applications("get_views")
        r = plug_core.build_sections_list(l)
        self.assert_(r, "build_sections_list returns nothing!")
        
    def test_get_view(self):
        from apps.core.models.section import Section
        s = Section.objects.all()[0]
        r = plug_core.get_view(s)
        self.assert_(r, "get_view returns nothing!")
        s.viewtype = ""
        r = plug_core.get_view(s)
        self.assert_(not r, "get_view returns something with an empty section")
    
    def test_get_view_for_elem(self):
        e = Element.objects.all()[0]
        r = plug_core.get_view_for_elem(e)
        self.assert_(r, "get_view_for_elem returns nothing!")
        e.etype = ""
        r = plug_core.get_view_for_elem(e)
        self.assert_(not r, "get_view_for_elem returns something with an empty etype")

    def test_get_input_for_elem(self):
        r = plug_core.get_input_for_elem("witch:Booking")
        self.assert_(r, "get_input_for_elem returns nothing")

    def test_get_form_for_etype(self):
        r = plug_core.get_form_for_etype("witch:Booking")
        self.assert_(r, "get_form_for_etype returns nothing")


    def test_elem_relative_section(self):
        r = plug_core.elem_relative_section("witch:Offer")
        self.assert_(r, "elem_relative_section returns nothing")

        
